package com.example.demo.controller;

import com.example.demo.entity.Laboratory;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private  UserService userService;


    @PostMapping("/login")
    public User login(@RequestBody User user){
        if(user.getUsername()==null||user.getPassword()==null){
            return null;
        }
        return userService.getLongIn(user.getUsername(), user.getPassword());

    }

    /**
     * 注册用户（新增用户信息）
     * @param user
     * @return
     */
    @PostMapping("/register")
    public User register(@RequestBody  User user){
        return userService.register(user);
    }

    /**
     * 条件查询
     * @param user
     * @return
     */
    @PostMapping("/list")
    public List<User> list(@RequestBody User user){
        return userService.list(user);
    }

    /**
     * 条件删除 根据ID
     * @param user
     * @return
     */
    @PostMapping("/delete")
    public int delete(@RequestBody   User user){
        if(user.getId()==null){
            return 0;
        }
        return userService.delete(user.getId());
    }


    /**
     * genju ID用户 用于修改获取修改前的数据
     * @param user
     * @return
     */
    @PostMapping("/getById")
    public User getById(@RequestBody  User user){
        if(user.getId()==null){
            return null;
        }
        return userService.getById(user.getId());
    }
}
